package scsc.web.system.actions;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.servlet.ServletException;
import scsc.pub.exception.UnifyUserException;
import scsc.web.system.Constants;
import scsc.web.system.forms.SysOperatorForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpSession;
import scsc.pub.data.UserContext;
import scsc.pub.db.DBManage;
import scsc.pub.tools.StringUtil;
import scsc.pm.SysOperation;
import scsc.pub.db.PageListData;

public class SelectOperatorAction extends Action {
	public SelectOperatorAction() {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException, UnifyUserException {

		SysOperatorForm actionForm = (SysOperatorForm) form;
		String fwd = null;
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession();
		UserContext userContext = (UserContext) session
				.getAttribute("userContext");
		DBManage dbmanage = null;
		SysOperation operator = new SysOperation();

		String selectId = StringUtil.trim(actionForm.getSelectId());
		String name = StringUtil.trim(actionForm.getSelName());
		String selectType = StringUtil.trim(actionForm.getSelectType());
		String selectDeptCode = StringUtil.trim(actionForm.getSelectDeptCode());
		String operatorIDs = StringUtil.trim(actionForm.getOperatorIDs());
		String operatorNames = StringUtil.trim(actionForm.getOperatorNames());

		String operAtt = actionForm.getOperAtt();
		if (operAtt == null || StringUtil.trim(operAtt).equals("")) {
			operAtt = Constants.DB_Select;
			actionForm.setOperAtt(operAtt);
		}
		if (operAtt.equals("selectMulti")) {
			if (!operatorIDs.equals("") && !operatorNames.equals("")) {
				Vector OperatorList = getOperatorList(actionForm
						.getOperatorIDs(), actionForm.getOperatorNames());
				request.setAttribute("OperatorList", OperatorList);
			}
		}
		PageListData footer = actionForm.getFooter();
		dbmanage = new DBManage();
		operator.selectOperators(dbmanage, selectId, selectDeptCode,
				selectType, name, footer);
		dbmanage.close();
		if (operAtt.equals("selectMulti")) {
			fwd = "selectMultiOperator";
		} else {
			fwd = "selectOperator";
		}

		// return data from database and set foreign key list
		// setGroupList(request, userContext, groupLocal, errors);

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
		}

		return mapping.findForward(fwd);
	}

	private Vector getOperatorList(String operatorID, String operatorName) {
		Vector AddresseeList = new Vector();
		if (!operatorID.equals("") && !operatorName.equals("")) {
			StringTokenizer addresseeto = new StringTokenizer(operatorID, ",");
			StringTokenizer addresseeNameto = new StringTokenizer(operatorName,
					",");
			while (addresseeto.hasMoreElements()
					&& addresseeNameto.hasMoreElements()) {
				AddresseeList.add(new LabelValueBean(addresseeNameto
						.nextToken(), addresseeto.nextToken()));
			}
		}
		return AddresseeList;
	}

}
